package filesystem3;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * <p>FVStatistics</p>
 * <p>Description: CS343 Group Project</p>
 * @author Ken Peck <ken.peck@my.uwrf.edu>
 * @author Jason Skinner <jason.skinner@my.uwrf.edu>
 * @author Robbie Fox <robert.fox@my.uwrf.edu>
 * @date   November 24th, 2014
 * 
 * <p>The <code>FVStatistics</code> class has methods to calculate statistics and store details about files.
 */

public class FVStatistics {

	private static int fileCount = 0;
	private String details = "no details";
	private static Map<String, Integer> fileTypesMap = new HashMap<String, Integer>();

	/*
	 *  constructors
	 */
	public FVStatistics(){	
		fileCounter();
	}

	public FVStatistics(String details){
		setDetails(details);
		//XMLWriter.add();
	}

	/*
	 *  method counts number of files opened
	 */

	public void fileCounter(){
		fileCount++;
	}

	/*
	 *  query the number of files opened
	 *  @return an int of the number of files opened
	 */
	public int getCount(){
		return fileCount;
	}

	/*
	 *  file details - set user comments about a file and store them in xml for later retrieval
	 */
	public void setDetails(String s){
		s = this.details;
	}

	/*
	 *  file details - get user comments about a file from xml
	 *  @param String input from user with details about the file
	 *  
	 */
	public String getDetails(String s){
		return details;
	}

	/*
	 * @param takes in a String with the filename
	 * @return returns the extension as a String
	 */
	public String getExtension(String fileName) {
		String extension = "null";
		int period = fileName.lastIndexOf('.');
		if (period > 0) {
			extension = fileName.substring(period + 1);
		}
		return extension;
	}
	
	/*
	 *  finds the most used file type
	 *  @param String filename
	 *  @return String most used file type extension
	 */
	public String mostPopular(String s){
		String fileType = getExtension(s);

		if (fileTypesMap.containsKey(fileType)){
			fileTypesMap.put(fileType, fileTypesMap.get(fileType) +1 );
		}
		else{
			fileTypesMap.put(fileType, 1);
		}
		
		Entry<String,Integer> maxEntry = null;

		for(Entry<String,Integer> entry : fileTypesMap.entrySet()) {
		    if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
		        maxEntry = entry;
		    }
		}
			
		return maxEntry.getKey();
	}
}



